//
//  HDMessageTableViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/16.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDMessageTableViewController.h"

@interface HDMessageTableViewController ()

@end

@implementation HDMessageTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"msgCell";
    HDMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell==nil) {
        cell = [[HDMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
    }
    cell.message = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [HDMessageCell cellHeight:self.dataArray[indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

@end

@interface HDMessageCell ()
@property(nonatomic,strong)UILabel *unreadLabel;
@property(nonatomic,strong)UITextView *msgTextView;
@property(nonatomic,strong)UILabel *timeLabel;
@end

@implementation HDMessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.unreadLabel];
        [self.contentView addSubview:self.msgTextView];
        [self.contentView addSubview:self.timeLabel];
    }
    return self;
}

- (void)setMessage:(HDMessageInfo *)value
{
    _message = [value copy];
    self.unreadLabel.hidden = !_message.isUnread;
    self.msgTextView.text = _message.msgContent;
    CGFloat height = [HDUtil heightForTextViewSize:CGSizeMake(APPFRAME.size.width-34, CGFLOAT_MAX) WithText:_message.msgContent font:[UIFont systemFontOfSize:13]];
    self.msgTextView.height = height;
    self.timeLabel.text = _message.time;
    self.timeLabel.top = self.msgTextView.bottom+5;
}

+(CGFloat)cellHeight:(HDMessageInfo *)message
{
    CGFloat height = 17;
    CGFloat textViewHeight = [HDUtil heightForTextViewSize:CGSizeMake(APPFRAME.size.width-34, CGFLOAT_MAX) WithText:message.msgContent font:[UIFont systemFontOfSize:13]];
    height+=textViewHeight+35;
    return height;
}

#pragma mark - getters
- (UILabel *)unreadLabel
{
    if (!_unreadLabel) {
        _unreadLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 7, 7)];
        _unreadLabel.layer.cornerRadius = 3.5;
        _unreadLabel.backgroundColor = [UIColor hexStringToColor:@"#ff4400"];
        _unreadLabel.clipsToBounds = YES;
        _unreadLabel.hidden = YES;
    }
    return _unreadLabel;
}

- (UITextView *)msgTextView
{
    if (!_msgTextView) {
        _msgTextView = [[UITextView alloc] initWithFrame:CGRectMake(self.unreadLabel.right, self.unreadLabel.bottom, APPFRAME.size.width-34, 20)];
        _msgTextView.textColor = [UIColor hexStringToColor:@"#222222"];
        _msgTextView.editable = NO;
        _msgTextView.scrollEnabled = NO;
        _msgTextView.dataDetectorTypes = UIDataDetectorTypeAll;
        _msgTextView.font = [UIFont systemFontOfSize:13];
    }
    return _msgTextView;
}

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.unreadLabel.right, self.msgTextView.bottom+10, APPFRAME.size.width-34, 20)];
        _timeLabel.textColor = [UIColor hexStringToColor:@"#222222"];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = [UIFont systemFontOfSize:13];
    }
    return _timeLabel;
}

@end
